const VueSSRClientPlugin = require('vue-server-renderer/client-plugin')
const VueSSRServerPlugin = require('vue-server-renderer/server-plugin')
const nodeExternals = require('webpack-node-externals')
const env = process.env
const hasRunEnv = !!env.RUN_ENV
const isServer = env.RUN_ENV === 'server'
const isDev = process.env.NODE_ENV === 'development'
module.exports = {
  publicPath: '/',
  outputDir: hasRunEnv ? `dist/${env.RUN_ENV}` : 'dist', 
  assetsDir: 'assets',
  lintOnSave: isDev,
  productionSourceMap: false,
  devServer: {
    open: true,
    overlay: {
      warnings: false,
      errors: true
    },
    proxy: {
      [process.env.VUE_APP_BASE_API]: {
        target: `${process.env.VUE_APP_SERVER_HOST}/`,
        secure: false,
        changeOrigin: true
      }
    }
  },
  css: {
    loaderOptions: {
      scss: {
        prependData: `@import "~@/styles/global.scss";
        `
      }
    }
  },
  configureWebpack: !hasRunEnv ? {} : {
    entry: `./ssr/entry-${env.RUN_ENV}.js`,
    devtool: "source-map",
    target: isServer ? "node" : "web",
    // 此处告知 server bundle 使用 Node 风格导出模块(Node-style exports)
    output: {
      libraryTarget: isServer ? "commonjs2" : undefined,
    },
    externals: isServer
      ? nodeExternals({
          // 不要外置化 webpack 需要处理的依赖模块。
          // 你可以在这里添加更多的文件类型。例如，未处理 *.vue 原始文件，
          // 你还应该将修改 `global`（例如 polyfill）的依赖模块列入白名单
          allowlist: [/\.css$/, /vant\/lib/],
        })
      : undefined,
    optimization: { splitChunks: isServer ? false : undefined },
    plugins: [
      isServer ? new VueSSRServerPlugin() : new VueSSRClientPlugin()
    ]
  },
  chainWebpack (config) {
    config.plugins.delete('preload')
    config.plugins.delete('prefetch')

    config.when(process.env.NODE_ENV === 'development', config => config.devtool('cheap-source-map'))

    // 修改vue-cli3关于图片打包的配置，设定打包限制为10kb 默认4kb
    /* config.module
      .rule('images')
      .use('url-loader')
      .loader('url-loader')
      .options({
        limit: 10240
      }) */
  }
}
